package com.seaboxdata.util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;

/*
 * AES对称加密和解密
 */
public class SymmetricEncoderUtil {
    
    private static final String characterEncoding       = "UTF-8";
    private static final String cipherTransformation    = "AES/ECB/PKCS5PADDING";
    private static final String aesEncryptionAlgorithem = "AES";
    
    
    /**
     * Method for Encrypt Plain String Data
     * @param plainText
     * @return encryptedText
     */
        public static String AESEncode(String encrptyKey,String plainText) {
    	
        String encryptedText = "";
        try {
        	if (encrptyKey.length()>16)
        		encrptyKey=encrptyKey.substring(0,16);
            Cipher cipher   = Cipher.getInstance(cipherTransformation);
            byte[] key      = encrptyKey.getBytes(characterEncoding);
            SecretKeySpec secretKey = new SecretKeySpec(key, aesEncryptionAlgorithem);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] cipherText = cipher.doFinal(plainText.getBytes(characterEncoding));
            Base64.Encoder encoder = Base64.getEncoder();
            encryptedText = encoder.encodeToString(cipherText);

        } catch (Exception E) {
             System.err.println("Encrypt Exception : "+E.getMessage());
        }
        return encryptedText;
    }

    /**
     * Method For Get encryptedText and Decrypted provided String
     * @param encryptedText
     * @return decryptedText
     */
    public static String AESDncode(String encrptyKey,String encryptedText) {
        String decryptedText = "";
        try {
        	if (encrptyKey.length()>16)
        		encrptyKey=encrptyKey.substring(0,16);
            Cipher cipher = Cipher.getInstance(cipherTransformation);
            byte[] key = encrptyKey.getBytes(characterEncoding);
            SecretKeySpec secretKey = new SecretKeySpec(key, aesEncryptionAlgorithem);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] cipherText = decoder.decode(encryptedText.getBytes(characterEncoding));
            decryptedText = new String(cipher.doFinal(cipherText), characterEncoding);

        } catch (Exception E) {
            System.err.println("decrypt Exception : "+E.getMessage());
        }
        return decryptedText;
    }
    
    /**
     * Method for Encrypt Plain String Data
     * @param plainText
     * @return encryptedText
     */
    public static String AESUrlSafeEncode(String encrptyKey,String plainText) {
        String encryptedText = "";
        try {
        	if (encrptyKey.length()>16)
        		encrptyKey=encrptyKey.substring(0,16);
            Cipher cipher   = Cipher.getInstance(cipherTransformation);
            byte[] key      = encrptyKey.getBytes(characterEncoding);
            SecretKeySpec secretKey = new SecretKeySpec(key, aesEncryptionAlgorithem);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] cipherText = cipher.doFinal(plainText.getBytes(characterEncoding));
            Base64.Encoder encoder = Base64.getUrlEncoder();
            encryptedText = new String(encoder.encode(cipherText),characterEncoding);

        } catch (Exception E) {
             System.err.println("Encrypt Exception : "+E.getMessage());
        }
        return encryptedText;
    }

    /**
     * Method For Get encryptedText and Decrypted provided String
     * @param encryptedText
     * @return decryptedText
     */
    public static String AESUrlSafeDncode(String encrptyKey,String encryptedText) {
        String decryptedText = "";
        try {
        	if (encrptyKey.length()>16)
        		encrptyKey=encrptyKey.substring(0,16);
            Cipher cipher = Cipher.getInstance(cipherTransformation);
            byte[] key = encrptyKey.getBytes(characterEncoding);
            SecretKeySpec secretKey = new SecretKeySpec(key, aesEncryptionAlgorithem);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            Base64.Decoder decoder = Base64.getUrlDecoder();
            byte[] cipherText = decoder.decode(encryptedText.getBytes(characterEncoding));
            decryptedText = new String(cipher.doFinal(cipherText), characterEncoding);

        } catch (Exception E) {
        	E.printStackTrace();
            System.err.println("decrypt Exception : "+E.getMessage());
        }
        return decryptedText;
    }
    
}